package com.tanhua.manage.service;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.common.enums.UserStatusEnum;
import com.tanhua.manage.mapper.UserFreezesMapper;
import com.tanhua.manage.mapper.UserStatusMapper;
import com.tanhua.common.pojo.UserFreeze;
import com.tanhua.manage.pojo.UserStatus;
import com.tanhua.manage.vo.UsersFreezeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserFreezeService {

    @Autowired
    private UserFreezesMapper userFreezesMapper;

    @Autowired
    private UserStatusMapper userStatusMapper;

    /**
     * @param userId             用户id
     * @param freezingTime       冻结时间
     * @param freezingRange      冻结范围
     * @param reasonsForFreezing 冻结原因
     * @param frozenRemarks      冻结备注
     * @return
     */
    public UsersFreezeVo UserFreeze(Long userId, Integer freezingTime, Integer freezingRange, String reasonsForFreezing, String frozenRemarks) {

        //记录当前冻结时间戳
        long time = System.currentTimeMillis();

        //点击冻结之后往冻结表里添加一条数据
        UserFreeze userFreeze = new UserFreeze(null, userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks,time);
        int insert = userFreezesMapper.insert(userFreeze);

        //将状态表字段改为冻结
        UpdateWrapper<UserStatus> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        UserStatus userStatus = new UserStatus(null, userId, UserStatusEnum.freeze);
        int update = userStatusMapper.update(userStatus, updateWrapper);



            //判断是否为空 为空就失败
            UsersFreezeVo usersFreezeVo = new UsersFreezeVo();
        if (ObjectUtil.isAllNotEmpty(insert, update)) {
            String message = "操作成功";
            usersFreezeVo.setMessage(message);
            return usersFreezeVo;
        }
        return null;
    }
}
